﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.IO;

namespace Gauss.Isp
{
    class Utils
    {
        #region String Formatting

        public static string StringFormat(string format, params object[] args)
        {
            return string.Format(CultureInfo.CurrentCulture, format, args);
        }

        public static string StringFormatNoCulture(string format, params object[] args)
        {
            return string.Format(CultureInfo.InvariantCulture, format, args);
        }

        #endregion

        #region Integer Parsing

        public static int ParseInt(string value)
        {
            return int.Parse(value, CultureInfo.CurrentCulture);
        }

        public static bool TryParseInt(string value, out int result)
        {
            return int.TryParse(value, NumberStyles.Integer, CultureInfo.CurrentCulture, out result);
        }

        #endregion

        #region MessageBox

        private static IWin32Window _messageBoxOwner = null;

        public static IWin32Window MessageBoxOwner
        {
            get { return _messageBoxOwner; }
            set { _messageBoxOwner = value; }
        }

        public static DialogResult MessageBox(Exception exception, string text)
        {
            return MessageBox(exception, text, null, MessageBoxButtons.OK, MessageBoxIcon.None);
        }

        public static DialogResult MessageBox(Exception exception, string text, MessageBoxIcon icon)
        {
            return MessageBox(exception, text, null, MessageBoxButtons.OK, icon);
        }

        public static DialogResult MessageBox(Exception exception, string text, string caption)
        {
            return MessageBox(exception, text, caption, MessageBoxButtons.OK, MessageBoxIcon.None);
        }

        public static DialogResult MessageBox(Exception exception, string text, string caption, MessageBoxButtons buttons)
        {
            return MessageBox(exception, text, caption, buttons, MessageBoxIcon.None);
        }

        public static DialogResult MessageBox(Exception exception, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            if (string.IsNullOrEmpty(caption))
            {
                caption = "ISP Model Checker";
            }

            if (exception != null)
            {
                try
                {
                    string logPath = Path.Combine(Path.GetTempPath(), "isp-vs.log");
                    using (StreamWriter sw = new StreamWriter(logPath, true))
                    {
                        sw.WriteLine("--------------------------------------------------------------------------------");
                        sw.WriteLine("[" + DateTime.Now.ToLongDateString() + " - " + DateTime.Now.ToLongTimeString() + "]");
                        sw.WriteLine("Title: " + caption);
                        sw.WriteLine("Message: " + text);
                        sw.WriteLine();
                        sw.WriteLine(GenerateExceptionString(exception));
                        sw.WriteLine("--------------------------------------------------------------------------------");
                        sw.WriteLine();
                    }
                }
                catch { }

#if DEBUG
                text = Utils.StringFormat("{0}\n\n{1}", text, GenerateExceptionString(exception));
#endif
            }

            if (MessageBoxOwner != null && MessageBoxOwner is Control && (MessageBoxOwner as Control).InvokeRequired)
            {
                Control c = MessageBoxOwner as Control;
                return (DialogResult)
                    c.Invoke(new MessageBoxInvoker(System.Windows.Forms.MessageBox.Show),
                             MessageBoxOwner, text, caption, buttons, icon);
            }
            else
            {
                return System.Windows.Forms.MessageBox.Show(MessageBoxOwner, text, caption, buttons, icon);
            }
        }

        private delegate DialogResult MessageBoxInvoker(IWin32Window owner,
                                                                             string text,
                                                                             string caption,
                                                                             MessageBoxButtons buttons,
                                                                             MessageBoxIcon icon);

        private static string GenerateExceptionString(Exception exception)
        {
            if (exception == null)
            {
                return "";
            }

            StringBuilder exceptionString = new StringBuilder();
            exceptionString.Append(Utils.StringFormat("Exception: {0}\nMessage: {1}\nStack Trace:\n{2}", exception.GetType().Name, exception.Message, exception.StackTrace));
            if (exception.InnerException != null)
            {
                exceptionString.Append("\n\nInner Exception:\n");
                exceptionString.Append(GenerateExceptionString(exception.InnerException));
            }

            return exceptionString.ToString();
        }

        #endregion
    }
}
